# 32. 电脑病毒感染

32 32-1

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

const lines = [];
rl.on('line', (line) => {
    lines.push(line);
});
rl.on('close', () => {
    const N = parseInt(lines[0], 10);
    const connections = parseInt(lines[1], 10);
    const times = [];

    for(let i=0; i<connections; i++) {
        const line = lines[i+2].split(' ').map(Number);
        times.push(line[0]-1, line[1]-1, line[2]);
    }
    const initial = parseInt(lines[connections+2]) - 1;
    console.log(networkDelayTime(times, N, initial));
})

function networkDelayTime(time, N, K) {
    const INF = Infinity;
    const dist = new Array(N).fill(INF);
    disk[K] = 0;
    for(let i=0; i<N; i++) {
        for(const [u,v,w] of times) {
            if (dist[u]+w < dist[v]) {
                dist[v] = dist[u] + w;
            }
        }
    }

    let maxWait = 0;
    for(let i=0; i<N; i++) {
        if(dist[i] === INF) return -1;
        maxWait = Math.max(maxWait, dist[i]);
    }
    return maxWait;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42